# coding=utf-8
# opencv-python, numpy, matplotlib

import socket
import cv2 as cv

# 视频流目的地址
import numpy as np

UDP_DEST_ADDR = ("127.0.0.1", 8080)
# 发送缓冲区 1MB
# UDP_BUFF_SIZE = 1024*1024
udp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# UDP编程时最好不要超过1472B(针对以太网)，要求不高也不可以超过65507B，即不超过64KB
# 发送缓冲区 64KB
# print 'udp_send_size: ', udp_client.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
# udp_client.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, UDP_BUFF_SIZE)
# print 'udp_send_size: ', udp_client.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)

cap = cv.VideoCapture(1)
# print cap.get(cv.CAP_PROP_FRAME_WIDTH), cap.get(cv.CAP_PROP_FRAME_HEIGHT)
while True:
    ret, frame = cap.read()
    # print type(frame), len(frame)
    frame = cv.resize(frame, (160, 120))
    # print type(frame[0]), len(frame[0]), type(frame[0][0]), len(frame[0][0]), type(frame[0][0][0]), len(frame)
    # 数据量 57600B 56.25KB
    data = frame.tobytes()
    # print type(data[0]), len(data[0]), data[0]
    udp_client.sendto(data, UDP_DEST_ADDR)
    # cv.imshow("video_client", frame)
    # if cv.waitKey(1) & 0xFF == ord('q'):
    #     break

